package com.hckjsec.cloud.controller;

import java.util.List;

import com.hckjsec.common.utils.DateUtils;
import com.hckjsec.common.utils.SecurityUtils;
import com.hckjsec.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hckjsec.common.annotation.Log;
import com.hckjsec.common.core.controller.BaseController;
import com.hckjsec.common.core.domain.AjaxResult;
import com.hckjsec.common.enums.BusinessType;
import com.hckjsec.cloud.domain.WebApplicationProduct;
import com.hckjsec.cloud.service.IWebApplicationProductService;
import com.hckjsec.common.utils.poi.ExcelUtil;
import com.hckjsec.common.core.page.TableDataInfo;

/**
 * 公司网站产品Controller
 * 
 * @author cyt
 * @date 2021-09-22
 */
@RestController
@RequestMapping("/web/product")
public class WebApplicationProductController extends BaseController
{
    @Autowired
    private IWebApplicationProductService webApplicationProductService;

    /**
     * 查询公司网站产品列表
     */
    @GetMapping("/list")
    public TableDataInfo list(WebApplicationProduct webApplicationProduct)
    {
        startPage();
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            webApplicationProduct.setDeptId(SecurityUtils.getDeptId());
        }
        List<WebApplicationProduct> list = webApplicationProductService.selectWebApplicationProductList(webApplicationProduct);
        list.forEach(kv->{
            kv.setContent(null);
        });
        return getDataTable(list);
    }

    /**
     * 导出公司网站产品列表
     */
    @Log(title = "公司网站产品", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WebApplicationProduct webApplicationProduct)
    {
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            webApplicationProduct.setDeptId(SecurityUtils.getDeptId());
        }
        List<WebApplicationProduct> list = webApplicationProductService.selectWebApplicationProductList(webApplicationProduct);
        ExcelUtil<WebApplicationProduct> util = new ExcelUtil<WebApplicationProduct>(WebApplicationProduct.class);
        return util.exportExcel(list, "product");
    }

    /**
     * 获取公司网站产品详细信息
     */
    @GetMapping(value = "/getproduct/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(webApplicationProductService.selectWebApplicationProductById(id));
    }

    /**
     * 新增公司网站产品
     */
    @Log(title = "公司网站产品", businessType = BusinessType.INSERT)
    @PostMapping("/addproduct")
    public AjaxResult add(@RequestBody WebApplicationProduct webApplicationProduct)
    {
        int rows =0;
        if (StringUtils.isNull(webApplicationProduct.getId())){
            rows=webApplicationProductService.insertWebApplicationProduct(webApplicationProduct);
        }else{
            webApplicationProduct.setUpdatedAt(DateUtils.parseDate(DateUtils.getTime()));
            rows=webApplicationProductService.updateWebApplicationProduct(webApplicationProduct);
        }
        return toAjax(rows);
    }

    /**
     * 删除公司网站产品
     */
    @Log(title = "公司网站产品", businessType = BusinessType.DELETE)
	@DeleteMapping("/delproduct/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(webApplicationProductService.deleteWebApplicationProductByIds(ids));
    }
}
